﻿package GUI {
	import flash.display.MovieClip;
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import caurina.transitions.Tweener;
	import code.Field;
	
	/**
	 * ...
	 * @author Sam Morrison
	 */
	public class Title extends MovieClip{
		private var options:Array = new Array();
		private var selected:int;
		private var active:String = "play";
		public var play_mc:MovieClip;
		public var instructions_mc:MovieClip;
		public var credits_mc:MovieClip;
		
		public function Title() {
			options.push("play");
			options.push("instructions");
			options.push("credits");
			
			addEventListener(Event.ENTER_FRAME, update);
			Keyboarder.Instance.addEventListener(KeyboarderEvent.FIRST_PRESS, pressed);
		}
		
		private function pressed(e:KeyboarderEvent):void {
			if ((e.keyCode == 83
			|| e.keyCode == 76)
			&& selected < options.length - 1){
				selected++;
			}
			
			if ((e.keyCode == 87
			|| e.keyCode == 79)
			&& selected > 0){
				selected--;
			}
			
			switch(active)
			{
				case "play":
					switch (options[selected]) 
					{
						case "play":
							if (e.keyCode == 69)
							{
								active = "play";
								(parent as code.Field).gameInit();
							}
						break;
						
						case "instructions":
							if (e.keyCode == 69)
							{
								Tweener.addTween(this, { y: -648, alpha:1, time:1.0, transition:"easeOutQuint" } );
								active = "instructions";
							}
						break;
						
						case "credits":
							if (e.keyCode == 69)
							{
								Tweener.addTween(this, { y:720, alpha:1, time:1.0, transition:"easeOutQuint" } );
								active = "credits";
							}
						break;
					}
				break;
				
				case "credits":
					if (e.keyCode == 69)
					{
						Tweener.addTween(this, { y:0, alpha:1, time:1.0, transition:"easeOutQuint" } );
						active = "play";
					}
				break;
				
				case "instructions":
					if (e.keyCode == 69)
					{
						Tweener.addTween(this, { y:-1272, alpha:1, time:1.0, transition:"easeOutQuint" } );
						active = "instructions1";
					}
				break;
				
				case "instructions1":
					if (e.keyCode == 69)
					{
						Tweener.addTween(this, { y:-1926, alpha:1, time:1.0, transition:"easeOutQuint" } );
						active = "instructions2";
					}
				break;
				
				case "instructions2":
					if (e.keyCode == 69)
					{
						Tweener.addTween(this, { y:0, alpha:1, time:2.0, transition:"easeOutQuint" } );
						active = "play";
					}
				break;
			
			}
		}
		
		private function update(e:Event):void {
			switch(active)
			{
				case "play":
					switch (options[selected]) 
					{
						case "play":
							play_mc.gotoAndStop(2);
							instructions_mc.gotoAndStop(1);
							credits_mc.gotoAndStop(1);
						break;
						
						case "instructions":
							play_mc.gotoAndStop(1);
							instructions_mc.gotoAndStop(2);
							credits_mc.gotoAndStop(1);
						break;
						
						case "credits":
							play_mc.gotoAndStop(1);
							instructions_mc.gotoAndStop(1);
							credits_mc.gotoAndStop(2);
						break;
					}
					//if(kb.pressed(
				break;
				
				case "credits":
				
				break;
				
				case "instructions":
				
				break;
			
			}
		}
		
		public function remove():void {
			Keyboarder.Instance.removeEventListener(KeyboarderEvent.FIRST_PRESS, pressed);
			removeEventListener(Event.ENTER_FRAME, update);
		}
		
	}

}